package com.xiaozhengkeji.minecraft.plugin.fishplus;

import com.google.gson.Gson;
import com.xiaozhengkeji.minecraft.plugin.fishplus.config.FishConfig;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishPrizeEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishRodEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.utils.MsgUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public final class FishPlus extends JavaPlugin {
    public static String PluginsVersion = "1.0";
    public static String PluginsName = "FishPlus";
    public static com.xiaozhengkeji.minecraft.plugin.fishplus.FishPlus Main;
    public static String MCVersion;
    public static Boolean Dev = true;
    public static Boolean PluginStartState = false;

    /**
     * 插件被读取
     */
    @Override
    public void onLoad() {
        super.onLoad();
        MsgUtils.logServer("插件[" + PluginsName + "]开始读取中");
        saveDefaultConfig();
        saveResource("FishPrizeConfig.yml", Dev);
        saveResource("FishRodConfig.yml", Dev);
        MCVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
        MsgUtils.logServer("插件[" + PluginsName + "]读取完成");
    }

    /**
     * 插件被加载
     */
    @Override
    public void onEnable() {
        super.onEnable();
        Main = this;
        MsgUtils.logServer("插件[" + PluginsName + "]开始加载中");
        configRead();
        RegisterSystem.infoSystem();
        RegisterSystem.registerCommands();
        RegisterSystem.registerHook();
        RegisterSystem.registerListener();
        if (Dev) {
            RegisterSystem.registerDev();
        }
        if (PluginStartState) {
            MsgUtils.logServer("插件[" + PluginsName + "]加载完成");
        } else {
            MsgUtils.logServer("§插件[" + PluginsName + "]加载失败");
            Bukkit.getPluginCommand("FishPlus").getPlugin().onDisable();
        }

    }

    /**
     * 插件被卸载
     */
    @Override
    public void onDisable() {
        super.onDisable();
        MsgUtils.logServer("§插件[" + PluginsName + "]卸载完成!");
    }

    /*插件配置读取*/
    public void configRead() {
        FishConfig.CROSS_SERVER = getConfig().getBoolean("CrossServer.Enable");
        if (FishConfig.CROSS_SERVER) { // 判断是否开启数据跨服
            FishConfig.PATH_STRING_PLAYER_DATA = getConfig().getString("CrossServer.DataFilePath");
            MsgUtils.logServer("开启数据跨服 - > " + FishConfig.PATH_STRING_PLAYER_DATA);
        }
        File temp_player_data = new File(FishConfig.PATH_STRING_PLAYER_DATA);
        if (!temp_player_data.exists()) {
            MsgUtils.logServer("数据目录不存在!");
            if (temp_player_data.mkdir()) {
                MsgUtils.logServer("以为您创建数据目录! -> " + FishConfig.PATH_STRING_PLAYER_DATA);
                PluginStartState = true;
            } else {
                MsgUtils.logServer("§4创建数据目录! 失败!Error -> " + FishConfig.PATH_STRING_PLAYER_DATA);
                PluginStartState = false;
            }

        }else {
            PluginStartState = true;
        }
        //读取配置
        YamlConfiguration temp_fish_rod = YamlConfiguration.loadConfiguration(new File(FishConfig.PATH_STRING_FISH_ROD));
        YamlConfiguration temp_fish_prize = YamlConfiguration.loadConfiguration(new File(FishConfig.PATH_STRING_FISH_PRIZE));
        for (String temp_key : temp_fish_rod.getKeys(false)) {
            FishRodEntity temp_fish_rod_pojo = new FishRodEntity(temp_key,
                    temp_fish_rod.getString(temp_key + ".Lore"),
                    temp_fish_rod.getInt(temp_key + ".Lucky"),
                    temp_fish_rod.getDouble(temp_key + ".Hit"));
            MsgUtils.logServer("添加鱼竿 [" + temp_key + "] 完成!");
            MsgUtils.logServer("data -> " + new Gson().toJson(temp_fish_rod_pojo));
            FishConfig.FISH_ROD_ENTITY_HASH_MAP.put(temp_key, temp_fish_rod_pojo);
        }

        for (String temp_key : temp_fish_prize.getKeys(false)) {
            FishPrizeEntity temp_fish_prize_pojo = new FishPrizeEntity(temp_key,
                    temp_fish_prize.getInt(temp_key + ".PrizeType"),
                    temp_fish_prize.getInt(temp_key + ".Lucky"),
                    temp_fish_prize.getBoolean(temp_key + ".Announcement.Enable"),
                    temp_fish_prize.getString(temp_key + ".Announcement.Content"),
                    temp_fish_prize.getStringList(temp_key + ".Command"));
            MsgUtils.logServer("添加奖品 [" + temp_key + "] 完成!");
            MsgUtils.logServer("data -> " + new Gson().toJson(temp_fish_prize_pojo));
            FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.put(temp_key, temp_fish_prize_pojo);
        }
        //请使用服务器配置的任意鱼竿进行钓鱼!当前手持鱼竿不在配置范围内!
        FishConfig.LANGUAGE_HASH_MAP.put("NoConfigurationRodWasUsedWhenFishing", getConfig().getString("Language.NoConfigurationRodWasUsedWhenFishing"));
        FishConfig.LANGUAGE_HASH_MAP.put("IllegalPhishing", getConfig().getString("Language.IllegalPhishing"));

        MsgUtils.logServer("Lang [" + FishConfig.LANGUAGE_HASH_MAP.get("NoConfigurationRodWasUsedWhenFishing") + "]");
        MsgUtils.logServer("Lang [" + FishConfig.LANGUAGE_HASH_MAP.get("IllegalPhishing") + "]");
        FishConfig.LuckyMax = getConfig().getInt("Lucky.Max",105);
        FishConfig.LuckyIncrease = getConfig().getInt("Lucky.Increase",3);
        MsgUtils.logServer("LuckyMax [" + FishConfig.LuckyMax + "]");
        MsgUtils.logServer("LuckyIncrease [" + FishConfig.LuckyIncrease + "]");
        MsgUtils.logServer("配置文件加载完成!");
    }


}
